from alipay import AliPay
import qrcode


alipay = AliPay(
    appid="90210dsad61",
    app_notify_url=None,  # 默认回调url
    app_private_key_string=open('./cert/alipay/app_private_key.pem').read(),
    alipay_public_key_string=open('./cert/alipay/alipay_public_key.pem').read(),
    sign_type="RSA2",
    debug=True  # 默认False
)


# # 调用alipay的api生成订单信息，返回参数
# order_string = alipay.api_alipay_trade_page_pay(
#     out_trade_no="20241dsa2809",  # 订单号
#     total_amount=str(10),  # 支付金额
#     subject="商品标题",  # 订单标题
#     return_url="http://your_domain.com/return_url/",  # 支付后同步通知地址
#     notify_url="http://your_domain.com/notify_url/"  # 支付后异步通知地址
# )

# a = "https://openapi-sandbox.dl.alipaydev.com/gateway.do?" + order_string

# print(a)


def get_qr_code(code_url):
    """
    生成二维码
    :param code_url:  创建预付订单时生成的code_url
    :return:
    """
    qr=qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=10,
        border=1,
    )
    qr.add_data(code_url) ##二维码所含信息(支付地址等)
    img=qr.make_image()   ##生成二维码图片
    img.save('/Users/apple/PycharmProjects/untitled1/Node_W/static/qr_test_ali.png')
    print('二维码保存成功')


result=alipay.api_alipay_trade_precreate(
    subject="我就是测试",
    out_trade_no="2024111515060912345",
    total_amount=0.5)
print('返回值',result)
code_url=result.get('qr_code')
